<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="'修改会员级别'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="110">
          <FormItem label="会员姓名">
            {{ form.data.nickName }}
          </FormItem>
          <FormItem prop="isShow" label="会员类型">
            <Select v-model="form.data.pkMemberTypeId">
              <Option v-for="item in typeList" :value="item.id" :key="item.id">{{ item.memberTypeName }}</Option>
            </Select>
          </FormItem>
          <FormItem prop="remark" label="备注">
            <Input type="textarea" v-model="form.data.remark" placeholder="请输入备注" :rows="4"></Input>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import { mapGetters } from 'vuex'
  import vipApi from '@/api/vip'

  export default {
    name: 'update-vip-modal',
    props: [
      'form'
    ],
    data () {
      return {
        loading: true,
        ruleValidate: {
        },
        typeList: []
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$refs.form.resetFields()
        } else {
          this.getMemberDetail()
        }
      },
      async getMemberDetail () {
        const { body } = await vipApi.memberDetail({
          pkRcId: this.userInfo.pkRclubInfo,
          pkMemberInfo: this.form.data.pkMemberInfo
        })
        const { responseData } = body
        if (this.$apiHelper.requestSuccessed(body.responseCode)) {
          this.form.data = responseData
        } else {
          setTimeout(() => {
            this.$Modal.error({
              title: '错误',
              content: body.desc,
              onOk: () => {}
            })
          }, 500)
        }
      },
      async getTypeList () {
        const { body } = await vipApi.typeList({
          pkRcId: this.userInfo.pkRclubInfo
        })
        const { responseData } = body
        this.typeList = responseData
      },
      async submitForm () {
        await vipApi.updateMemberType(this.form.data)
        this.$Message.info('修改成功')
        this.form.visible = false
        this.$emit('submit')
      }
    },
    async mounted () {
      await this.getTypeList()
    }
  }
</script>
